<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectCode">
              <a-input v-model="model.projectCode" placeholder="请输入项目编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
              <a-input v-model="model.projectName" placeholder="请输入项目名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目所在行政区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="regionCode">
              <j-dict-select-tag
                type="list"
                v-model="model.regionCode"
                dictCode="sys_xz_area"
                placeholder="请选择项目所在行政区域"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="投资项目统一代码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="investProjectCode"
            >
              <a-input v-model="model.investProjectCode" placeholder="请输入投资项目统一代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入项目地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目法人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="legalPerson">
              <a-input v-model="model.legalPerson" placeholder="请输入项目法人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目行业分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="industriesType">
              <j-category-select v-model="model.industriesType" pcode="A04" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="资金来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fundSource">
              <a-input v-model="model.fundSource" placeholder="请输入资金来源"></a-input>
            </a-form-model-item>
          </a-col>
          <!--
          <a-col :span="8">
            <a-form-model-item label="出资比例" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contributionScale">
              <a-input v-model="model.contributionScale" placeholder="请输入出资比例"></a-input>
            </a-form-model-item>
          </a-col>
          -->
          <a-col :span="8">
            <a-form-model-item label="项目规模" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectScale">
              <a-input v-model="model.projectScale" placeholder="请输入项目规模"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactor">
              <a-input v-model="model.contactor" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactInformation">
              <a-input v-model="model.contactInformation" placeholder="请输入联系方式"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="是否立项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="islixiang">
              <j-dict-select-tag type="radio" v-model="model.islixiang" dictCode="yn" placeholder="请选择是否立项" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="不立项原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="notlixiang">
              <a-input v-model="model.notlixiang" placeholder="请输入不立项原因"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="监督部门名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="superviseDeptName"
            >
              <a-input v-model="model.superviseDeptName" placeholder="请输入监督部门名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="监督部门代码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="superviseDeptCode"
            >
              <a-input v-model="model.superviseDeptCode" placeholder="请输入监督部门代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="项目审批文件名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="approvalName"
            >
              <a-input v-model="model.approvalName" placeholder="请输入项目审批文件名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目审批文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalNumber">
              <a-input v-model="model.approvalNumber" placeholder="请输入项目审批文号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="项目审批单位"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="approvalAuthority"
            >
              <a-input v-model="model.approvalAuthority" placeholder="请输入项目审批单位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachmentCode">
              <j-upload :projectUploadFlag="true" v-model="model.attachmentCode"></j-upload>
            </a-form-model-item>
          </a-col>
          <!--
          <a-col :span="8">
            <a-form-model-item label="交易系统标识码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="platformCode">
              <a-input v-model="model.platformCode" placeholder="请输入交易系统标识码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="国有资金（万元）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="stateOwnedFund"
            >
              <a-input-number v-model="model.stateOwnedFund" placeholder="请输入国有资金（万元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="国有资金比例%"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="stateOwnedFundRatio"
            >
              <a-input-number :min="0"
                v-model="model.stateOwnedFundRatio"
                placeholder="请输入国有资金比例%"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="私有资金（万元）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="privateFund"
            >
              <a-input-number v-model="model.privateFund" placeholder="请输入私有资金（万元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="私有资金比例%"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="privateFundRatio"
            >
              <a-input-number v-model="model.privateFundRatio" placeholder="请输入私有资金比例%"  :min="0" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="外国政府及组织投资（万元）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="foreignFund"
            >
              <a-input-number
                v-model="model.foreignFund"
                placeholder="请输入外国政府及组织投资（万元）"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="外国政府及组织投资比例"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="foreignFundRatio"
            >
              <a-input-number
                v-model="model.foreignFundRatio"
                placeholder="请输入外国政府及组织投资比例"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="境外私人投资（万元）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="foreignPrivateFund"
            >
              <a-input-number
                v-model="model.foreignPrivateFund"
                placeholder="请输入境外私人投资（万元）"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="境外私人投资比例%"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="foreignPrivateFundRatio"
            >
              <a-input-number :min="0"
                v-model="model.foreignPrivateFundRatio"
                placeholder="请输入境外私人投资比例%"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          -->
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <!-- <a-tab-pane tab="标段（包）" :key="refKeys[1]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="exSectionTable.loading"
          :columns="exSectionTable.columns"
          :dataSource="exSectionTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"
        />
      </a-tab-pane> -->
      <a-tab-pane tab="招标信息" :key="refKeys[0]" :forceRender="true">
        <ex-tender-project-form
          ref="exTenderProjectForm"
          @validateError="validateError"
          :disabled="formDisabled"
        ></ex-tender-project-form>
      </a-tab-pane>
    </a-tabs>
    <a-card title="标段信息" style="margin: 15px 0px;" :body-style="{ padding: '2px' }">
      <section-list ref="sectionListRef" :projectId="model.id" :disabled="formDisabled" @ok="sectionListOk" />
    </a-card>
    <ex-operation-record ref="exOperationRecord" :module="'exProject'" :entryId="model.id"></ex-operation-record>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { FormTypes, getRefPromise, VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
import { validateDuplicateValue } from '@/utils/util'
import ExTenderProjectForm from './ExTenderProjectForm.vue'
import SectionList from '@/views/exProject/section/SectionList'
import ExOperationRecord from '@/views/exOperationRecord/Index'
export default {
  name: 'ExProjectForm',
  mixins: [JEditableTableModelMixin],
  components: {
    ExTenderProjectForm,
    SectionList,
    ExOperationRecord,
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 10 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },
      model: {},
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {
        projectId: [{ required: true, message: '请输入项目标识码 (guid)!' }],
        projectCode: [{ required: true, message: '请输入项目编号!' }],
        projectName: [{ required: true, message: '请输入项目名称!' }],
        regionCode: [{ required: true, message: '请输入项目所在行政区域代码!' }],
        // investProjectCode: [{ required: true, message: '请输入投资项目统一代码!' }],
        // address: [{ required: true, message: '请输入项目地址!' }],
        // legalPerson: [{ required: true, message: '请输入项目法人!' }],
        // industriesType: [{ required: true, message: '请输入项目行业分类!' }],
        // fundSource: [{ required: true, message: '请输入资金来源!' }],
        // projectScale: [{ required: true, message: '请输入项目规模!' }],
        // contactor: [{ required: true, message: '请输入联系人!' }],
        // contactInformation: [{ required: true, message: '请输入联系方式!' }],
        // islixiang: [{ required: true, message: '请输入是否立项!' }],
        // superviseDeptName: [{ required: true, message: '请输入监督部门名称!' }],
        // superviseDeptCode: [{ required: true, message: '请输入监督部门代码!' }],
        // platformCode: [{ required: true, message: '请输入交易系统标识码!' }],
        // stateOwnedFund: [{ required: true, message: '请输入国有资金（万元）!' }],
        // stateOwnedFundRatio: [{ required: true, message: '请输入国有资金比例!' }],
        // privateFund: [{ required: true, message: '请输入私有资金（万元）!' }],
        // privateFundRatio: [{ required: true, message: '请输入私有资金比例!' }],
        // foreignFund: [{ required: true, message: '请输入外国政府及组织投资（万元）!' }],
        // foreignFundRatio: [{ required: true, message: '请输入外国政府及组织投资比例!' }],
        // foreignPrivateFund: [{ required: true, message: '请输入境外私人投资（万元）!' }],
        // foreignPrivateFundRatio: [{ required: true, message: '请输入境外私人投资比例!' }],
      },
      refKeys: ['exTenderProject'],
      tableKeys: ['exTenderProject'],
      activeKey: 'exTenderProject',
      // 招标项目
      exTenderProjectTable: {
        loading: false,
        dataSource: [],
        columns: [],
      },
      url: {
        add: '/exProject/exProject/add',
        edit: '/exProject/exProject/edit',
        queryById: '/exProject/exProject/queryById',
        exTenderProject: {
          list: '/exProject/exProject/queryExTenderProjectByMainId',
        },
      },
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {},
  methods: {
    addBefore() {
      this.$refs.exTenderProjectForm.clearFormData()
    },
    getAllTable() {
      let values = this.tableKeys.map((key) => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      this.$nextTick(() => {
        this.$refs.exTenderProjectForm.initFormData(this.url.exTenderProject.list, this.model.id)
      })
    },
    //校验所有一对一子表表单
    validateSubForm(allValues) {
      var that = this
      return new Promise((resolve, reject) => {
        Promise.all([this.$refs.exTenderProjectForm.validate(0)])
          .then(() => {
            resolve(allValues)
            that.save()
          })
          .catch((e) => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
      })
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        exTenderProjectList: this.$refs.exTenderProjectForm.getFormData(),
      }
    },
    validateError(msg) {
      this.$message.error(msg)
    },
    sectionListOk() {
      this.model.exSectionList = this.$refs.sectionListRef.dataSource
    },
    handleOk() {
      const that = this
      this.model.exTenderProjectList = this.$refs.exTenderProjectForm.getFormData()
      this.model.exSectionList = this.$refs.sectionListRef.dataSource
      if(this.model.exSectionList.length == 0){
        that.$message.warning("标段信息不能为空")
        return;
      }
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        that.validateSubForm(0)
        // debugger
        // var valid2 = that.$refs.exTenderProjectForm.validate2()
        // if (valid) {
        //   that.save()
        // }
      })
    },
    save() {
      var that = this
      that.confirmLoading = true
      let httpurl = ''
      let method = ''
      if (!this.model.id) {
        httpurl += this.url.add
        method = 'post'
      } else {
        httpurl += this.url.edit
        method = 'put'
      }
      httpAction(httpurl, this.model, method)
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.$emit('ok')
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
  },
}
</script>

<style scoped></style>
